layui.use(['form','layer','upload'], function () {
    var $ = layui.jquery, form = layui.form,layer=layui.layer,upload=layui.upload;
    var m = $('meta[name=\'_csrf\']').attr('content');
    var n = $('meta[name=\'_csrf_header\']').attr('content');
    var fwdl = "", fwfl = "";
    var msg=$("input[name='msg']").val();
    if(""!=msg&&undefined!=msg){
        layer.msg(msg);
    }

    /**
     * 服务分类
     */
    form.on('select(fwdl)', function (data) {
        fwdl = data.value;
        fwfl = "";
        if (""!= fwdl) {
            addFwdl(fwdl);
        }

    });
    form.on('select(bxlx)',function (data) {
        fwfl=data.value;
      $("input[name='gznr']").val("");
      $("#fwdlmc").addClass("gznr").text("点击选择故障描述");
    });
    $("#fwdlmc").on("click",function () {
        var gznr=$("input[name='gznr']").val();
        var fwdlmc=$("#fwdlmc").text();
        if("点击选择故障描述"==fwdlmc){
            fwdlmc="";
        }
        fwdl=$("select[name='fwdl']").val();
        fwfl=$("select[name='bxlx']").val();
        console.log(gznr);
        if(""==fwfl){
            layer.msg('请选择服务类型！', {icon: 5})
        }else {
            layer.open({
                type:2,
                area:['420px','540px'],
                shade: 0.8,
                shadeClose: true,
                content:'/uc/apply/gznr/index?fwdl='+fwdl+"&fwfl="+fwfl+"&gznr="+gznr+"&fwdlmc="+fwdlmc+"&timespace="+new Date('yyyyMMddHHmmss')
            });
        }
    });
    /**
     * 添加服务类型
     * @param id
     */
    function addFwdl(id) {
        $.ajax({
            type: 'get',
            url: '/uc/apply/ajax/fwfl?id=' + id + "&timespace=" + new Date('yyyyMMddhhmmss'),
            dataType: 'json',
            async: false,
            beforeSend: function (xhr) {
                xhr.setRequestHeader(n, m)
            },
            contentType: 'application/json;charset=utf-8',
            error: function (request) {
                console.log('Connection error')
            },
            success: function (data) {
                if (data.state == 200) {
                    var dataList = data.dataList;
                    var html = "<option value=''>--请选择服务类型--</option>";
                    if (null != dataList && dataList.length > 0) {
                        for (var i = 0; i < dataList.length; i++) {
                            html = html + "<option value='" + dataList[i].id + "'>" + dataList[i].fwfl + "</option>";
                        }
                    }
                    $("select[name='bxlx']").html(html);
                    form.render('select');
                }
            }
        })
    }
    upload.render({
        elem:'#imagePc1',
        url:'/uc/upload/layer/input/image',
        title:"上传附件",
        headers:{'X-CSRF-TOKEN':m},
        ext : 'jpg|png|gif|zip',
        done: function(res) { //上传成功后的回调
            $("#customImage").attr("href",res.data.src).show();
            $("#customImage").text(res.data.src.substring(16,res.data.src.length));
            $("input[name='fj']").val(res.data.src);
        }
    });
    upload.render({
        elem:'#imagePc',
        url:'/uc/upload/layer/input/image',
        headers:{'X-CSRF-TOKEN':m},
        title:'修改附件',
        ext:'jpg|png|gif|zip',
        done: function(res) { //上传成功后的回调
            $("#customImage").attr("href",res.data.src);
            $("#customImage").text(res.data.src.substring(16,res.data.src.length));
            $("input[name='fj']").val(res.data.src);
        }
    });

    $(".custom-save").on("click",function () {
        $(".custom-form").submit();
    });
});
    